package xy.donate.android.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import xy.donate.android.R;
import xy.donate.android.aop.SingleClick;
import xy.donate.android.common.MyActivity;
import xy.donate.android.constant.Constant;
import xy.donate.android.helper.MmkvHelper;
import xy.donate.android.ui.dialog.AddressDialog;
import xy.donate.android.ui.dialog.InputDialog;

import com.bumptech.glide.Glide;
import com.hjq.widget.layout.SettingBar;

/**
 * author : Xy
 * desc   : 个人资料
 */
public final class PersonalDataActivity extends MyActivity {

    private ViewGroup mAvatarLayout;
    private ImageView mAvatarView;
    private SettingBar mIDView;
    private SettingBar mNameView;
    private SettingBar mAddressView;

    /**
     * 省
     */
    private String mProvince = "重庆市";
    /**
     * 市
     */
    private String mCity = "重庆市";
    /**
     * 区
     */
    private String mArea = "南岸区";

    /**
     * 头像地址
     */
    private String mAvatarUrl;

    @Override
    protected int getLayoutId() {
        return R.layout.personal_data_activity;
    }

    @Override
    protected void initView() {
        mAvatarLayout = findViewById(R.id.fl_person_data_avatar);
        mAvatarView = findViewById(R.id.iv_person_data_avatar);
        mIDView = findViewById(R.id.sb_person_data_id);
        mNameView = findViewById(R.id.sb_person_data_name);
        mAddressView = findViewById(R.id.sb_person_data_address);
        setOnClickListener(mAvatarLayout, mAvatarView, mNameView, mAddressView);
    }

    @Override
    protected void initData() {
        String address = mProvince + mCity + mArea;
        if (MmkvHelper.getInstance().getMmkv().contains(Constant.USER_ADDRESS)) {
            address = MmkvHelper.getInstance().getMmkv().getString(Constant.USER_ADDRESS, address);
        }
        if (MmkvHelper.getInstance().getMmkv().contains(Constant.USER_HEADER)) {
            Glide.with(getActivity())
                    .load(MmkvHelper.getInstance().getMmkv().getString(Constant.USER_HEADER,""))
                    .placeholder(R.drawable.avatar_placeholder_ic)
                    .error(R.drawable.avatar_placeholder_ic)
                    .circleCrop()
                    .into(mAvatarView);
        } else {
            Glide.with(getActivity())
                    .load(R.drawable.avatar_placeholder_ic)
                    .placeholder(R.drawable.avatar_placeholder_ic)
                    .error(R.drawable.avatar_placeholder_ic)
                    .circleCrop()
                    .into(mAvatarView);
        }
        mAddressView.setRightText(address);
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        if (v == mAvatarLayout) {
            ImageSelectActivity.start(this, data -> {
                mAvatarUrl = data.get(0);
                Glide.with(getActivity())
                        .load(mAvatarUrl)
                        .into(mAvatarView);
                MmkvHelper.getInstance().getMmkv().encode(Constant.USER_HEADER, mAvatarUrl);
            });
        } else if (v == mAvatarView) {
            if (!TextUtils.isEmpty(mAvatarUrl)) {
                // 查看头像
                ImagePreviewActivity.start(getActivity(), mAvatarUrl);
            } else {
                // 选择头像
                onClick(mAvatarLayout);
            }
        } else if (v == mNameView) {
            new InputDialog.Builder(this)
                    // 标题可以不用填写
                    .setTitle(getString(R.string.personal_data_name_hint))
                    .setContent(mNameView.getRightText())
                    //.setHint(getString(R.string.personal_data_name_hint))
                    //.setConfirm("确定")
                    // 设置 null 表示不显示取消按钮
                    //.setCancel("取消")
                    // 设置点击按钮后不关闭对话框
                    //.setAutoDismiss(false)
                    .setListener((dialog, content) -> {
                        if (!mNameView.getRightText().equals(content)) {
                            mNameView.setRightText(content);
                            MmkvHelper.getInstance().getMmkv().encode(Constant.USER_NAME, content);
                        }
                    })
                    .show();
        } else if (v == mAddressView) {
            new AddressDialog.Builder(this)
                    //.setTitle("选择地区")
                    // 设置默认省份
                    .setProvince(mProvince)
                    // 设置默认城市（必须要先设置默认省份）
                    .setCity(mCity)
                    // 不选择县级区域
                    //.setIgnoreArea()
                    .setListener((dialog, province, city, area) -> {
                        String address = province + city + area;
                        MmkvHelper.getInstance().getMmkv().encode(Constant.USER_ADDRESS, address);
                        if (!mAddressView.getRightText().equals(address)) {
                            mProvince = province;
                            mCity = city;
                            mArea = area;
                            mAddressView.setRightText(address);
                        }
                    })
                    .show();
        }
    }
}